package com.kol.collect.service.admin.impl;

import java.util.Map;

import com.kol.collect.common.AjaxJson;
import com.kol.collect.common.util.FastJsonUtils;
import com.kol.collect.dao.DriverDao;
import com.kol.collect.model.Driver;
import com.kol.collect.service.admin.AdminDriverService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.TypeReference;

@Service
@Configuration
public class AdminDriverServiceImpl implements AdminDriverService {
    private static Logger logger = LoggerFactory.getLogger(AdminDriverServiceImpl.class);
    @Autowired
    private DriverDao driverDao;

    @Override
    public AjaxJson<String> save(String data) {

        AjaxJson<String> ajaxJson = new AjaxJson<>();
        ajaxJson.setStatus(AjaxJson.STATUS_UPDATE_FAILED);
        ajaxJson.setMsg(AjaxJson.MSG_UPDATE_FAILED);
        Map<String, String> dataMap = FastJsonUtils.toBean(data, new TypeReference<Map<String, String>>() {});

        String name = dataMap.get("name");
        String phone = dataMap.get("phone");
        String remarks = dataMap.get("remarks");
        try {
            Driver driver = new Driver();
            driver.setName(name);
            driver.setPhone(phone);
            driver.setRemarks(remarks);

            if (driverDao.save(driver) > 0) {
                ajaxJson.setStatus(AjaxJson.STATUS_SUCCESS);
                ajaxJson.setMsg(AjaxJson.MSG_SUCCESS);
            } else {
                logger.info("司机保存失败");
                ajaxJson.setStatus(AjaxJson.STATUS_EXCEPTION);
                ajaxJson.setMsg(AjaxJson.MSG_EXCEPTION);
                return ajaxJson;
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(AjaxJson.STATUS_EXCEPTION);
            ajaxJson.setMsg(AjaxJson.MSG_EXCEPTION);
            return ajaxJson;
        }
        return ajaxJson;
    }

}